package com.ht.aems.fesmodel.persist;

import com.ht.aems.fesmodel.RtMeasureQuery;
import com.ht.aems.fesmodel.RtMeasureSaver;
import com.ht.aems.fesmodel.data.MeasurePoint;
import com.ht.aems.fesmodel.data.YCData;
import com.ht.aems.fesmodel.data.YXData;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-8-6
 */
public class RtMeasureHandler_Map implements RtMeasureQuery, RtMeasureSaver {
    Map<Integer, Double> analogValues = new HashMap<Integer, Double>(1000);
    Map<Integer, Long> analogTimestamp = new HashMap<Integer, Long>(1000);
    Map<Integer, Integer> discreteValues = new HashMap<Integer, Integer>(1000);
    Map<Integer, Long> discreteTimestamp = new HashMap<Integer, Long>(1000);

    public boolean saveYC(YCData yc) {
        for (int i = 0; i < yc.getYcBit().length; i++) {
            analogValues.put(MeasurePoint.getNID(yc.getRemoteUnitId(), yc.getYcBit()[i]), yc.getData()[i]);
            analogTimestamp.put(MeasurePoint.getNID(yc.getRemoteUnitId(), yc.getYcBit()[i]), yc.getTimestamp());
        }
        return true;
    }

    public boolean saveYCs(YCData[] ycs) {
        for (YCData yc : ycs)
            saveYC(yc);
        return true;
    }

    public boolean saveYX(YXData yx) {
        for (int i = 0; i < yx.getYxBit().length; i++) {
            discreteValues.put(MeasurePoint.getNID(yx.getRemoteUnitId(), yx.getYxBit()[i]), yx.getData()[i]);
            discreteTimestamp.put(MeasurePoint.getNID(yx.getRemoteUnitId(), yx.getYxBit()[i]), yx.getTimestamp());
        }
        return true;
    }

    public boolean saveYXs(YXData[] yxs) {
        for (YXData yx : yxs)
            saveYX(yx);
        return true;
    }

    public Double getYCValue(int pointId) {
        return analogValues.get(pointId);
    }

    public Double getYCValueNotFes(int pointId) {
        return null;
    }

    public Double getYCValue(int rtuId, int pointNum) {
        return analogValues.get(MeasurePoint.getNID(rtuId, pointNum));
    }

    public Integer getYXValue(int pointId) {
        return discreteValues.get(pointId);
    }

    public Integer getYXValueNotFes(int pointId) {
        return null;
    }

    public Integer getYXValue(int rtuId, int pointNum) {
        return discreteValues.get(MeasurePoint.getNID(rtuId, pointNum));
    }

    public Long getYCTime(int pointId) {
        return analogTimestamp.get(pointId);
    }

    public Long getYXTime(int pointId) {
        return discreteTimestamp.get(pointId);
    }

    public Map<Integer, Double> getYCValues(int[] nIds) {
        Map<Integer, Double> result = new HashMap<Integer, Double>(nIds.length);
        for (int nid : nIds) {
            Double ycValue = getYCValue(nid);
            if (ycValue != null)
                result.put(nid, ycValue);
        }
        return result;
    }

    public Map<Integer, Double> getYCValuesNotFes(int[] nIds) {
        return null;
    }

    public Map<Integer, Integer> getYXValues(int[] nIds) {
        Map<Integer, Integer> result = new HashMap<Integer, Integer>(nIds.length);
        for (int nid : nIds) {
            Integer yxValue = getYXValue(nid);
            if (yxValue != null)
                result.put(nid, yxValue);
        }
        return result;
    }

    public Map<Integer, Integer> getYXValuesNotFes(int[] nIds) {
        return null;
    }
}
